using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed;
    public float rushSpeed;
    public float jumpSpeed;
    public GameObject jumpAir;
    private Rigidbody2D rb;
    private Animator anim;
    private Collider2D coll;
    private LayerMask ground;
    private LayerMask wall;
    public float jumpTimes,rushTimes;
    private bool canmove = true;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
        coll = GetComponent<Collider2D>();
        ground = LayerMask.GetMask("Ground");
        wall = LayerMask.GetMask("Wall");
    }

    // Update is called once per frame
    private void Update()
    {
        if (coll.IsTouchingLayers(ground))
        {
            jumpTimes = 1;
            rushTimes = 1;
        }
        if (canmove)
        {
            Movement();
            Rush();
        }
    }

    private void Movement()
    {
        /*float horizontalmove = Input.GetAxis("Horizontal");
        float facedircetion = Input.GetAxisRaw("Horizontal");
        if (horizontalmove != 0)
        {
            rb.velocity = new Vector2(horizontalmove * speed, rb.velocity.y);
        }
        if (facedircetion != 0)
        {
            transform.localScale = new Vector3(facedircetion, 1, 1);
        }*/
        if (Input.GetKey(UImanager.instance.D) && !Input.GetKey(UImanager.instance.A))
        {
            rb.velocity = new Vector2(speed, rb.velocity.y);
            transform.localScale = new Vector3(1, 1, 1);
        }
        else if (Input.GetKey(UImanager.instance.A) && !Input.GetKey(UImanager.instance.D))
        {
            rb.velocity = new Vector2(-speed, rb.velocity.y);
            transform.localScale = new Vector3(-1, 1, 1);
        }
        else if (Input.GetKeyUp(UImanager.instance.A)||Input.GetKeyUp(UImanager.instance.D))
        {
            rb.velocity = new Vector2(0, rb.velocity.y);
        }
        if (Input.GetKeyDown(UImanager.instance.K) && jumpTimes > 0)
        {
            if (!coll.IsTouchingLayers(ground))
            {
                Instantiate(jumpAir, transform.position + new Vector3(-0.0625f, -0.8f, 0), Quaternion.identity);
            }
            rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);
            jumpTimes -= 1;
        }
    }
    private void Rush()
    {
        if (Input.GetKeyDown(UImanager.instance.L) && rushTimes > 0)
        {
            rushTimes -= 1;
            canmove = false;
            StartCoroutine(Rushing());
        }
    }

    public IEnumerator Rushing()
    {
        canmove = false;
        float timer = .21f;
        float g = rb.gravityScale;
        //transform.position = new Vector3(transform.position.x, transform.position.y + .2f, transform.position.z);
        while (timer > 0.09f)
        {
            anim.Play("Rush");
            rb.velocity = new Vector2(transform.localScale.x * rushSpeed, 0);
            rb.gravityScale = 0;
            timer -= 0.03f;
            yield return new WaitForSeconds(0.03f);
        }
        timer = 0.09f;
        rb.velocity = new Vector2(0, 0);
        canmove = true;
        anim.Play("Idle");
        while (timer > 0)
        {
            rb.velocity = new Vector2(0, 0);
            timer -= 0.03f;
            rb.gravityScale = g;
            yield return new WaitForSeconds(0.03f);
        }
    }
}
